package cn.blue.pet.controller;
import cn.blue.org.domain.Employee;
import cn.blue.org.service.EmployeeService;
import cn.blue.pet.mapper.PetMapper;
import cn.blue.pet.service.IPetService;
import cn.blue.pet.domain.Pet;
import cn.blue.pet.query.PetQuery;
import cn.blue.base.util.AjaxResult;
import cn.blue.base.util.PageList;
import cn.blue.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    public IPetService petService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private PetMapper petMapper;


    /**
     * 保存和修改公用的
     *
     * @param pet 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet, HttpServletRequest request) {
        String token = request.getHeader("token");
        Logininfo logininfo = (Logininfo) redisTemplate.opsForValue().get("token");
//        Long id = logininfo.getId();
//        System.out.println(id);
//     Employee employee = employeeService.selectBylogininfo();
//     pet.setShopId(employee.getShopId());


        try {
            if (pet.getId() != null)
                petService.update(pet);
            else
                petService.add(pet);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            petService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Pet get(@PathVariable("id") Long id) {
        return petService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<Pet> list() {

        return petService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Pet> json(@RequestBody PetQuery query) {
        return petService.queryByPage(query);
    }

    //下架
    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long> ids) {
        petService.offsale(ids);
        return AjaxResult.me().success();
    }

    //上架
    @PostMapping("/onsale")
    public AjaxResult onSale(@RequestBody List<Long> ids) {
        petService.onsale(ids);
        return AjaxResult.me().success();
    }

    //根据宠物id查询详情
    @GetMapping("/goPetDetail/{id}")
    public Pet goPetDetail(@PathVariable("id") Long id) {
        return petService.goPetDetail(id);

    }

    @PostMapping("/dels")
    public AjaxResult buchdelete(@RequestBody List<Long> ids) {
        petService.batchDel(ids);
        return AjaxResult.me().success();
    }


}
